package com.mzy.linear_struct.subject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author: codermzy
 * @Date: 2024/04/22/下午3:53
 * @Description:
 */
public class 多数元素169 {
    public int majorityElement(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer value = entry.getValue();
            if (value > (nums.length / 2)) {
                return entry.getKey();
            }
        }
        return 0;
    }
}
